package com.bhs.sansonglogistics.ui.mine;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.event.Event;
import com.bhs.sansonglogistics.ui.home.MainActivity;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.bhs.sansonglogistics.view.FloatWindow;
import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;

/**
 * 新订单通知
 */
public class OrderInformActivity extends BaseActivity implements View.OnClickListener {

    private Switch mSwInvoice;
    private LinearLayout mLlBannerNo;
    private ImageView mIvBannerNo;
    private LinearLayout mLlBannerYes;
    private ImageView mIvBannerYes;
    private LinearLayout mLlFloatingWindowNo;
    private ImageView mIvFloatingWindowNo;
    private LinearLayout mLlFloatingWindowYes;
    private ImageView mIvFloatingWindowYes;
    private ActivityResultLauncher<Intent> intentActivityResultLauncher;

    @Override
    public int initLayout() {
        return R.layout.activity_order_inform;
    }

    @Override
    public void initView() {

        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("新订单通知");
        mSwInvoice = findViewById(R.id.sw_invoice);
        mLlBannerNo = findViewById(R.id.ll_banner_no);
        mIvBannerNo = findViewById(R.id.iv_banner_no);
        mLlBannerYes = findViewById(R.id.ll_banner_yes);
        mIvBannerYes = findViewById(R.id.iv_banner_yes);
        mLlFloatingWindowNo = findViewById(R.id.ll_floating_window_no);
        mIvFloatingWindowNo = findViewById(R.id.iv_floating_window_no);
        mLlFloatingWindowYes = findViewById(R.id.ll_floating_window_yes);
        mIvFloatingWindowYes = findViewById(R.id.iv_floating_window_yes);
        mLlBannerNo.setOnClickListener(this);
        mLlBannerYes.setOnClickListener(this);
        mLlFloatingWindowNo.setOnClickListener(this);
        mLlFloatingWindowYes.setOnClickListener(this);
        intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (Settings.canDrawOverlays(mContext)) {
                    if(type==1){
                        mIvBannerNo.setImageResource(R.mipmap.ic_unchecked);
                        mIvBannerYes.setImageResource(R.mipmap.ic_checked);
                        SharedPreferencesUtils.saveBoolean(mContext, "banner", true);
                    }else {
                        mIvFloatingWindowNo.setImageResource(R.mipmap.ic_unchecked);
                        mIvFloatingWindowYes.setImageResource(R.mipmap.ic_checked);
                        SharedPreferencesUtils.saveBoolean(mContext, "floatingWindow", true);
                    }
                }
            }
        });

    }

    @Override
    public void initData() {
        mSwInvoice.setChecked(SharedPreferencesUtils.getBoolean(this, "voice", true));
        if (SharedPreferencesUtils.getBoolean(mContext, "banner", false)) {
            mIvBannerNo.setImageResource(R.mipmap.ic_unchecked);
            mIvBannerYes.setImageResource(R.mipmap.ic_checked);
        }

        if (SharedPreferencesUtils.getBoolean(mContext, "floatingWindow", false)) {
            mIvFloatingWindowNo.setImageResource(R.mipmap.ic_unchecked);
            mIvFloatingWindowYes.setImageResource(R.mipmap.ic_checked);
        }
        mSwInvoice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferencesUtils.saveBoolean(mContext, "voice", b);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_banner_no:
                mIvBannerNo.setImageResource(R.mipmap.ic_checked);
                mIvBannerYes.setImageResource(R.mipmap.ic_unchecked);
                SharedPreferencesUtils.saveBoolean(mContext, "banner", false);
                break;
            case R.id.ll_banner_yes:
                requestDrawOverLays(1);
                break;
            case R.id.ll_floating_window_no:
                mIvFloatingWindowNo.setImageResource(R.mipmap.ic_checked);
                mIvFloatingWindowYes.setImageResource(R.mipmap.ic_unchecked);
                SharedPreferencesUtils.saveBoolean(mContext, "floatingWindow", false);
                break;
            case R.id.ll_floating_window_yes:
                requestDrawOverLays(2);
                break;
        }
    }

    private int type;

    public void requestDrawOverLays(int type) {
        this.type = type;
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
            intentActivityResultLauncher.launch(intent);
        } else {
            if(type==1){
                mIvBannerNo.setImageResource(R.mipmap.ic_unchecked);
                mIvBannerYes.setImageResource(R.mipmap.ic_checked);
                SharedPreferencesUtils.saveBoolean(mContext, "banner", true);
            }else {
                mIvFloatingWindowNo.setImageResource(R.mipmap.ic_unchecked);
                mIvFloatingWindowYes.setImageResource(R.mipmap.ic_checked);
                SharedPreferencesUtils.saveBoolean(mContext, "floatingWindow", true);
            }
        }
    }
}